﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EQual.Infrastructure.MVPFramework.Presenters
{
    public abstract class ListPresenterSimple <Tview,T>: ListPresenterDefault <Tview,T >  where Tview :Views .IListView <object ,object > where T :DataAccesLayer .DomainModel .BaseDataAccesObject 
    {

        public object ActiveItemSecondaryView { get; set; }


        protected virtual void InitializeView ()
        {
            View.ItemAdded += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object>>(View_ItemAdded);
            View.ItemSelected += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object>>(View_ItemSelected);
        }

        protected virtual void View_ItemSelected(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object> e)
        {

        }

        protected virtual void View_ItemAdded(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object> e)
        {

        }

        public override void Dispaly(T item)
        {
            this.View.Model = DoGetModel();
        }

        protected virtual  object DoGetModel()
        {
            return new { ItemsCollection = WrappedCollection()};
        }

        protected abstract object GetCollection();


        #region Collection wrraping 

        /// <summary>
        /// Wrappe up the collection so we can observe all the changes in collection
        /// </summary>
        /// <returns></returns>
        protected IEnumerable<T> WrappedCollection()
        {
            WpfApplicationExperimental.DAOCollection<T> items;
            System.Collections.Generic.IEnumerable<T> collection = GetCollection() as System.Collections.Generic.IEnumerable<T>;

            if (collection == null)
                items = new WpfApplicationExperimental.DAOCollection<T>();
            else
                items = new WpfApplicationExperimental.DAOCollection<T>(collection);

            items.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(CollectionChanged);

            return FilteredItems(items);
        }

        /// <summary>
        /// if more selection on the collection is needed .this is decorator and pipes and filters patterns
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        protected virtual IEnumerable<T> FilteredItems(WpfApplicationExperimental.DAOCollection<T> items)
        {
            return items;
        }

        protected virtual void CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {

        }

        #endregion

    }
}
